{% extends "admin_base.html" %}

{% block title %}Add a New Node{% endblock %}

{% block content %}
<div class="col-9">
	<h3 class="nopad">Add New Node</h3><hr />
	{% if get.disp is defined and get.disp != 1 %}
		<div class="alert alert-danger">
			{% if get.disp == 'agree_warn' %}
				You must agree to the node warning before we can create the node.
			{% elseif get.disp == 'missing_args' %}
				Not all arguments were passed by the script.
			{% elseif get.disp == 'n_fail' %}
				The node name does not meet the requirements (1-15 characters, a-zA-Z0-9_.-).
			{% elseif get.disp == 'url_fail' %}
				The node URL provided is not valid. URLs must end with a trailing slash and must be a subdomain without any additional folders. (e.g. <strong>http://node.example.com/</strong>)
			{% elseif get.disp == 'ip_fail' %}
				The IP addresses provided were not valid.
			{% elseif get.disp == 'dir_fail' %}
				The directory you entered was not valid. Must end with a trailing slash.
			{% elseif get.disp == 'ip_port_space' %}
				Unable to allocate the ports you inputted.
			{% elseif get.disp == 'port_fail' %}
				The ports you entered were not in a valid format.
			{% endif %}
		</div>
	{% endif %}
	<form action="ajax/new/create.php" method="POST">
		<fieldset>
			<div class="well">
				<div class="row">
					<div class="form-group col-6 nopad">
						<label for="node_name" class="control-label">Node Short Name</label>
						<div>
							<input type="text" name="node_name" placeholder="shortname" class="form-control" />
							<p class="text-muted" style="margin: 0 0 -10.5px;"><small><em>15 character maximum (a-zA-Z0-9_-.).</em></small></p>
						</div>
					</div>
					<div class="form-group col-6 nopad-right"></div>
				</div>
				<div class="row">
					<div class="form-group col-6 nopad">
						<label for="fqdn" class="control-label">Node <abbr title="Fully Qualified Domain Name">FQDN</abbr></label>
						<div>
							<input type="text" name="fqdn" class="form-control" placeholder="us1.nodes.example.com" />
							<p class="text-muted" style="margin: 0 0 -10.5px;"><small><em>Please enter the FQDN for this node. If you do not have one please enter its IP address. (Do not include the http:// or www)</em></small></p>
						</div>
					</div>
					<div class="form-group col-6 nopad-right">
						<label for="ip" class="control-label">Node IP Address</label>
						<div>
							<input type="text" name="ip" class="form-control" placeholder="255.255.255.0" />
							<p class="text-muted" style="margin: 0 0 -10.5px;"><small><em>Please enter the IP address of the node here. This IP should be publicly accessable. Using a private IP address will make servers inaccessable from an outside network.</em></small></p>
						</div>
					</div>
				</div>
			</div>
			<div class="well">
				<div class="row">
					<div class="form-group col-6 nopad">
						<label for="gsd_listen" class="control-label">GSD Listening Port</label>
						<div>
							<input type="text" name="gsd_listen" class="form-control" value="8003" />
						</div>
					</div>
					<div class="form-group col-6 nopad-right">
						<label for="gsd_console" class="control-label">GSD Console Port</label>
						<div>
							<input type="text" name="gsd_console" class="form-control" value="8031" />
						</div>
					</div>
				</div>
				<div class="row">
					<div class="form-group col-6 nopad">
						<label for="gsd_server_dir" class="control-label">GSD Server Directory</label>
						<div>
							<input type="text" name="gsd_server_dir" class="form-control" value="/home/" />
							<p class="text-muted" style="margin: 0 0 -10.5px;"><small><em>Include a trailing slash. Please make sure that this directory exists on the server.</em></small></p>
						</div>
					</div>
				</div>
			</div>
			<div class="well">
				<div class="row">
					<div class="form-group">
						<label for="ip_port" class="control-label">Available IPs &amp; Ports</label>
						<div>
							<textarea name="ip_port" class="form-control" rows="5" placeholder="127.0.0.1|25565,25566,25580-25590,25598,25599"></textarea>
							<p class="text-muted" style="margin: 0 0 -10.5px;"><small><em>Enter one IP address per line, followed by a pipe (|) and then a list of each available port separated with commas OR with a range of ports.</em></small></p>
						</div>
					</div>
				</div>
			</div>
			<div class="alert alert-danger"><input type="checkbox" name="read_warning" /> I have read and understand how to set up this node. (<a href="http://docs.pffr.me/en/latest/installing_nodes/" target="_blank">docs</a>)</div>
			<input type="submit" value="Create Node" id="disable_complete" class="btn btn-primary btn-sm disabled" />
		</fieldset>
	</form>
</div>
{% endblock %}

{% block javascript %}
<script type="text/javascript">
	$(document).ready(function(){
		$("input[name='node_name']").keyup(function(){
			if($(this).val().length > 15 || /^[a-zA-Z0-9_.-]*$/.test($(this).val()) == false)
				$(this).parent().parent().removeClass('has-success').addClass('has-error');
			else if($(this).val().length == 0)
				$(this).parent().parent().removeClass('has-success').removeClass('has-error');
			else
				$(this).parent().parent().removeClass('has-error').addClass('has-success');
		});
		$("input[name='read_warning']").click(function(){
			if($("input[name='read_warning']").is(":checked"))
				$("#disable_complete").removeClass("disabled");
			else
				$("#disable_complete").addClass("disabled");
		});
		if($.urlParam('error') != null){
			var field = $.urlParam('error');
			var exploded = field.split('|');
				$.each(exploded, function(key, value) {
					$('[name="' + value + '"]').parent().parent().addClass('has-error');
				});
			var obj = $.parseJSON($.cookie('__TMP_pp_admin_newnode'));
				$.each(obj, function(key, value) {
					$('[name="' + key + '"]').val(value);
				});
		}
	});
</script>
{% endblock %}
